package net.hzlj.platform.enterprise.task;

import android.util.Log;

import com.orhanobut.logger.Logger;

import net.hzlj.platform.enterprise.BootApp;
import net.hzlj.platform.enterprise.beans.resp.RespBaseBean;
import net.hzlj.platform.enterprise.utils.GsonUtils;
import net.hzlj.platform.enterprise.utils.StringUtils;

import java.io.File;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.FileDownloadCallback;
import cn.finalteam.okhttpfinal.HttpCycleContext;
import cn.finalteam.okhttpfinal.HttpRequest;
import okhttp3.Headers;

/**
 * @Description: 网络请求文件下载
 * @Author: Jason Zhang
 * @Date: 2016-8-31 17:21:46
 */
public class HttpRequestDownloadTask<T> extends Task {

    public File file;
    public String fileName;

    private static final String TAG = HttpRequestDownloadTask.class.getSimpleName();

    public HttpRequestDownloadTask(TaskListener listener, Class<T> tClass){
        super(listener,tClass);
    }

    public HttpRequestDownloadTask(TaskListener listener, HttpCycleContext httpCycleContext){
        super(listener,httpCycleContext);
    }

    public HttpRequestDownloadTask(TaskListener listener, HttpCycleContext httpCycleContext, Class<T> tClass){
        super(listener,httpCycleContext,tClass);
    }


    @Override
    public void execute() {
        final String filePath = file.getAbsolutePath();
        Logger.d("url:" + url + "\nfilePath:" + filePath);
        HttpRequest.download(url,file,new FileDownloadCallback(){
            @Override
            public void onProgress(int progress, long networkSpeed) {
                super.onProgress(progress, networkSpeed);
//                Log.i(TAG,"progress:" + progress + ",networkSpeed:" + networkSpeed);
            }

            @Override
            public void onDone() {
                super.onDone();
                respData = filePath;
                isSuccess = true;
                listener.onTaskResult(HttpRequestDownloadTask.this);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                isSuccess = false;
                listener.onTaskResult(HttpRequestDownloadTask.this);
            }
        });

    }

}
